<?php
/**
 * 内容管理
 * Class ContentController
 * @author 楚羽幽 <Name_Cyu@Foxmail.com>
 */
namespace Admin\Controller;
class VideoController extends AuthController
{
    private $cate, $cid, $vid, $db;

    /**
     * [$authAction 权限验证的动作]
     * @var array
     */
    private $authAction = array('add', 'edit', 'del');

    /**
     * [__init 构造函数]
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->cate = S('cate');
        $this->db = D('Video');
        $this->cid = I('cid');
        $this->vid = I('vid');

        //验证栏目cid
        if ($this->cid && !isset($this->cate[$this->cid]))
        {
            $this->error('栏目不存在');
        }
        //验证权限
        /*if (!$this->checkAccess()) {
            $this->error('没有操作权限');
        }*/
    }

    /**
     * [index 内容栏目选择页]
     * @return [type] [description]
     */
    public function index()
    {
        $this->display();
    }

    /**
     * [show 内容列表]
     * @return [type] [description]
     */
    public function show()
    {
        // 视频状态
        $status = I('video_status', 1, 'intval');
        // 分页类
        $page = new \Think\Page($this->db->count(), 15);
        $page = $page->show();
        $data = $this->db->relation(TRUE)->where(array('cid'=> $this->cid, 'video_status'=> $status))->limit($page->firstRow.','.$page->listRows)->select();
        $this->assign('page', $page);
        $this->assign('field', $data);
        $this->display();
    }

    /**
     * [AddVideo 上传视频]
     * 楚羽幽 <Name_Cyu@Foxmail.com>
     */
    public function AddVideo(){
        if(IS_POST){
            if($data = $this->db->CreateAdd()){
                $this->redirect('editVideo',array('cid'=> $_POST['cid'],'vid'=> $data));
            }else{
                $this->error($this->db->getError());
            }
        }else{
            $this->display();
        }
    }

    /**
     * [edit 修改视频]
     * @return [type] [description]
     */
    public function EditVideo(){
        if(IS_POST){
            if($this->db->CreateEdit()){
                $this->success('视频修改成功！', U('show',array('cid'=> $this->cid)));
            }else{
                $this->error($this->db->getError());
            }
        }else{
            $cate = $this->cate;
            $result = $this->db->relation(TRUE)->where(array('vid'=> $this->vid))->find();
            $this->assign('cate', $cate);
            $this->assign('field', $result);
            $this->display();
        }
    }

    /**
     * 删除内容视频
     */
    public function del()
    {
        if(M('video')->where(array('vid'=> $this->vid))->delete())
        {
            $this->success('视频内容删除成功！', U('show',array('cid'=> $this->cid)));
        }
        else
        {
            $this->error('视频内容删除失败！');
        }
    }

    /*-----------------------------------------------属性定义----------------------------------------------------*/

    /**
     * [UploadVideo 上传视频]
     * 楚羽幽 <Name_Cyu@Foxmail.com>
     */
    public function UploadVideo(){
        if (!empty($_FILES)){
            $cmd ="ffmpeg.exe -i ". $_FILES['Filedata']['name'] ." -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 1.mp4";
            $_FILES['Filedata']['name'] = exec($cmd);
            p($_FILES);die;
            // 上传设置
            $config = array(   
                'maxSize'    =>    99999999999, 
                'rootPath'   =>    './Upload/',
                'savePath'   =>    'Videos/',  
                'saveName'   =>    array('date','Y-m-dH-i-s'), 
                'exts'       =>    array('mp4', 'avi', 'flv', 'wmv'),
                'autoSub'    =>    true,   
                'subName'    =>    array('date','Ymd'),
            );
            $upload = new \Think\Upload($config);// 实例化上传类
            $images = $upload->upload();
            // 判断是否有值
            if($images){
                $info = $images['Filedata']['savename'];
                //返回文件地址和名给JS作回调用
                echo $info;
            }
            else{
                $this->error($upload->getError());// 获取失败信息
            }
        }
    }

    /**
     * [DelVideo 删除视频]
     * 楚羽幽 <Name_Cyu@Foxmail.com>
     */
    public function DelVideo(){

    }

    /**
     * [getCateZtree 获得栏目数据]
     * @return [type] [输出json数据]
     * 楚羽幽 <Name_Cyu@Foxmail.com>
     */
    public function getCateZtree()
    {
        if($this->cate)
        {
            $data = self::setZtree($this->cate);
        }
        $data = isset($data) ? $data : array();
        echo json_encode($data);die;
    }

    /**
     * [setZtree 拆分栏目数据并返回给getCateZtree]
     * @param [type]  $field [description]
     * @param integer $pid   [description]
     */
    static private function setZtree($field, $pid = 0){
        $cate = array();
        if ($field){
            foreach($field as $n => $cat){
                $data = array();
                // 过滤掉外部链接栏目
                if ($cat['cattype'] != 3){
                    // 普通频道
                    if($cat['cattype'] == 1){
                        $data = "<li><a href=\"" .U('show', array('cid' => $cat['cid'])) . "\" style=outline:none;>" . "<i class='icon-bars'></i> " . $cat['catname'] . "</a></li>";
                        $cate[] = $data;
                    }
                }
            }
        }
        return $cate;
    }


    /**
     * 验证操作权限
     * @return [type] [description]
     */
    /*public function checkAccess()
    {
        //管理员不验证
        if (IS_SUPER_ADMIN || IS_WEBMASTER)
        {
            return true;
        }
        if (in_array(ACTION, $this->authAction)) {
            $db = M('category_access');
            $access = $db->find($this->cid);
            //栏目没有权限
            if (empty($access)) {
                return true;
            }
            //获得角色权限
            $map['cid'] = $this->cid;
            $map['rid'] = $_SESSION['user']['rid'];
            $RoleAccess = $db->where($map)->find();
            if (empty($RoleAccess)) {
                return false;
            }
            return $RoleAccess[ACTION];
        }
    }*/

    /**
     * 排序
     */
    /*public function order()
    {
        $arc_order = Q('arc_order');
        if (!empty($arc_order) && is_array($arc_order))
        {
            $ContentModel = ContentModel::getInstance($this->mid);
            foreach ($arc_order as $vid => $order)
            {
                $ContentModel->save(array('vid' => $vid, 'arc_sort' => $order));
            }
        }
        $this->success('排序成功');
    }*/


    /**
     * 审核视频
     */
    public function audit()
    {
        $vid = I('vid', 0, 'intval');
        $status = I('video_status', 0, 'intval');
        $data = array('vid' => $vid, 'video_status' => $status);
        if($this->db->where(array('vid'=> $vid))->save($data))
        {
            $this->success('操作成功！', U('show',array('cid'=> $this->cid)));
        }
        else
        {
            $this->error('参数错误');
        }
    }

    /**
     * 移动视频
     */
    public function move()
    {
        if (IS_POST) {
            $ContentModel = ContentModel::getInstance($this->mid);
            //移动方式  1 从指定ID  2 从指定栏目
            $from_type = Q("post.from_type", 0, "intval");
            //目标栏目cid
            $to_cid = Q("post.to_cid", 0, 'intval');
            if ($to_cid) {
                switch ($from_type) {
                    case 1 :
                        //移动vid
                        $vid = Q("post.vid", 0, "trim");
                        $vid = explode("|", $vid);
                        if ($vid && is_array($vid)) {
                            foreach ($vid as $id) {
                                if (is_numeric($id)) {
                                    $ContentModel->save(array("vid" => $id, "cid" => $to_cid));
                                }
                            }
                        }
                        break;
                    case 2 :
                        //来源栏目cid
                        $from_cid = Q("post.from_cid", 0);
                        if ($from_cid) {
                            foreach ($from_cid as $d) {
                                if (is_numeric($d)) {
                                    $table = $this->model[$this->cate[$d]['mid']]['table_name'];
                                    M($table)->where("cid=$d")->save(array("cid" => $to_cid));
                                }
                            }
                        }
                        break;
                }
                $this->success('移动成功！');
            } else {
                $this->error('请选择目录栏目');
            }

        } else {
            $category = array();
            foreach ($this->cate as $n => $v) {
                //排除非本模型或外部链接类型栏目或单视频栏目
                if ($v['mid'] != $this->mid || $v['cattype'] == 3 || $v['cattype'] == 4) {
                    continue;
                }
                if ($this->cid == $v['cid']) {
                    $v['selected'] = "selected";
                }
                //封面栏目
                if ($v['cattype'] == 2) {
                    $v['disabled'] = 'disabled';
                }
                $category[$n] = $v;
            }
            $this->assign('category', $category);
            $this->display();
        }
    }



    /**
     * [uploadFile 上传文件]
     * @return [type] [description]
     */
    public function uploadFile()
    {
        $cache = S('field' . $_GET['mid']);
        $set = $cache[$_GET['name']]['set'];
        switch ($_GET['type']) {
            case 'thumb':
                //缩略图字段
                $set['type'] = 'thumb';
                $set['name'] = 'thumb';
                $set['allow_size'] = 2000;
                $set['num'] = 1;
                $set['filetype'] = '*.gif; *.jpg; *.png';
                break;
            case 'image':
                $set['type'] = 'image';
                $set['name'] = $_GET['name'];
                $set['allow_size'] *= 1000;
                $set['num'] = 1;
                $set['filetype'] = '*.gif; *.jpg; *.png';
                break;
            case 'images':
                $set['type'] = 'images';
                $set['name'] = $_GET['name'];
                $set['allow_size'] *= 1000;
                $set['filetype'] = '*.gif; *.jpg; *.png';
                break;
            case 'files':
                $set['type'] = 'files';
                $set['name'] = $_GET['name'];
                $set['allow_size'] *= 1000;
                $filetype = explode(',', $set['filetype']);
                $set['filetype'] = '';
                foreach ($filetype as $t) {
                    $set['filetype'] .= '*.' . $t . ';';
                }
                $set['filetype'] = substr($set['filetype'], 0, -1);
                break;
        }
        $this->assign('set', $set);
        $this->display();
    }

    /**
     * 站内文件
     */
    public function webFile()
    {
        $type = Q('type');
        switch ($type) {
            case 'file':
                $map['status'] = array('EQ', 1);
                break;
            case 'thumb':
            case 'image':
            case 'images':
                $map['status'] = array('EQ', 1);
                $map['image'] = array('EQ', 1);
                break;
            case 'files':
                $map = '';
                break;
        }
        $db = M('upload');
        $map['uid'] = $_SESSION['user']['uid'];
        $count = $db->where($map)->count();
        $page = new Page($count, 18);
        $data = $db->where($map)->limit($page->limit())->all();
        $this->assign('data', $data);
        $this->assign('page', $page->show());
        $this->display();
    }


    /**
     * 站内文件
     */
    public function noUse()
    {
        $type = Q('type');
        switch ($type) {
            case 'file':
                $map['status'] = array('EQ', 0);
                break;
            case 'thumb':
            case 'image':
            case 'images':
                $map['status'] = array('EQ', 0);
                $map['image'] = array('EQ', 1);
                break;
            case 'files':
                $map = '';
                break;
        }
        $db = M('upload');
        $map['uid'] = $_SESSION['user']['uid'];
        $count = $db->where($map)->count();
        $page = new Page($count, 18);
        $data = $db->where($map)->limit($page->limit())->all();
        $this->assign('data', $data);
        $this->assign('page', $page->show());
        $this->display();
    }

    /**
     * 批量删除
     */
    public function batchDel()
    {
        $vid = Q('vid', '', '');
        if (empty($vid)) {
            $this->error('请选择视频');
        }
        $ContentModel = new Content();
        foreach ($vid as $id) {
            if (!$ContentModel->del($id)) {
                $this->error($ContentModel->error);
            }
        }
        $this->success('删除成功');
    }
}